package org.eclipse.gmf.internal.bridge.wizards;

import org.eclipse.core.resources.IFile;
import org.eclipse.gmf.internal.bridge.wizards.pages.NewModelPage;
import org.eclipse.core.resources.IFile;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.gmf.gmfgraph.presentation.GMFGraphModelWizard;
import org.eclipse.gmf.gmfgraph.provider.GMFGraphEditPlugin;
import org.eclipse.gmf.internal.bridge.resolver.StructureResolver;
import org.eclipse.gmf.internal.bridge.ui.Plugin;
import org.eclipse.gmf.internal.bridge.wizards.pages.simple.DefinitionPage;
import org.eclipse.gmf.internal.bridge.wizards.pages.simple.DomainModelSelectionPage;
import org.eclipse.gmf.internal.bridge.wizards.pages.simple.DomainModelSourceImpl;
import org.eclipse.gmf.internal.bridge.wizards.pages.simple.GraphDefBuilder;
import org.eclipse.gmf.internal.bridge.wizards.pages.simple.StructureBuilder;
import org.eclipse.gmf.internal.bridge.wizards.pages.simple.ToolDefBuilder;
import org.eclipse.gmf.internal.common.URIUtil;
import org.eclipse.gmf.internal.common.ui.ModelSelectionPage;
import org.eclipse.gmf.internal.common.ui.ResourceLocationProvider;
import org.eclipse.gmf.tooldef.ToolRegistry;
import org.eclipse.gmf.tooldef.presentation.GMFToolModelWizard;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.gmf.internal.common.ui.ModelSelectionPage;

/**
 * @author dstadnik
 */
public class GMFNewModelWizard extends GMFToolModelWizard {

	protected NewModelPage page;
	
	protected DefinitionPage toolingDefinitionPage;
	
	protected ModelSelectionPage toolingModelSelectionPage;
	
	protected WizardOperationMode mode = new WizardOperationMode("gmftool", WizardOperationMode.CREATE); //$NON-NLS-1$
	
	public boolean performFinish() {
		return true;
	}
	
	public void setMode(String mode) {
		this.mode.setMode(mode);
	}
	
	protected boolean isInReconcileMode() {
		return toolingModelSelectionPage != null && toolingModelSelectionPage.getResource() != null;
	}
	
	protected EObject createInitialModel() {
		ToolDefBuilder builder = new ToolDefBuilder(null);
		return builder.process(toolingDefinitionPage.getModel());
	}

	protected void reconcileModel() {
		ToolRegistry existingToolRegistry = null;
		for (EObject element : toolingModelSelectionPage.getResource().getContents()) {
			if (element instanceof ToolRegistry) {
				existingToolRegistry = (ToolRegistry) element;
				break;
			}
		}
		ToolDefBuilder builder = new ToolDefBuilder(existingToolRegistry);
		ToolRegistry toolRegistry = builder.process(toolingDefinitionPage.getModel());
		if (toolRegistry != null && existingToolRegistry != toolRegistry) {
			toolingModelSelectionPage.getResource().getContents().add(toolRegistry);
		}
	}

	public void addPages() {
		ResourceLocationProvider rloc = new ResourceLocationProvider(selection);
		ResourceSet resourceSet = new ResourceSetImpl();
		page = new NewModelPage("NewModelPage", rloc, resourceSet);
		addPage(page);
	}

	public IFile getModelFile() {
		if (isInReconcileMode()) {
			return URIUtil.getFile(toolingModelSelectionPage.getResource().getURI());
		}
		return super.getModelFile();
	}
}
